package com.shuqing.dao;

import com.github.pagehelper.Page;
import com.shuqing.domain.Book;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

public interface BookMapper {
    @Select("select * from book where book_status !='3' order by book_uploadtime DESC")
    @Results(id = "bookMap", value = {
            //id 字段默认为false，表示不是主键
            //column 对应数据库字段名，property 对应实体类属性名称
            @Result(id = true, column = "book_id", property = "id"),
            @Result(column = "book_name", property = "name"),
            @Result(column = "book_isbn", property = "isbn"),
            @Result(column = "book_press", property = "press"),
            @Result(column = "book_author", property = "author"),
            @Result(column = "book_pagination", property = "pagination"),
            @Result(column = "book_price", property = "price"),
            @Result(column = "book_uploadtime", property = "uploadTime"),
            @Result(column = "book_status", property = "status"),
            @Result(column = "book_borrower", property = "borrower"),
            @Result(column = "book_borrowtime", property = "borrowTime"),
            @Result(column = "book_returntime", property = "returnTime"),
    })
    Page<Book> selectNewBooks();

    @Select("select * from book where book_id=#{id}")
    @ResultMap("bookMap")
    Book findById(Integer id);

    Integer editBook(Book book);

    //分页查询图书
    @Select({"<script>"+
            "select * from book where book_status != 3"+
            "<if test = \"name != null\">\n"+
            "   and book_name like concat('%',#{name},'%') \n"+
            "   </if>"+
            "<if test = \"press != null\">\n"+
            "   and book_press like concat('%',#{press},'%') \n"+
            "   </if>"+
            "<if test = \"author != null\">\n"+
            "   and book_author like concat('%',#{author},'%') \n"+
            "   </if>"+
            "order by book_borrowtime desc"+
            "</script>"
    })
    @ResultMap("bookMap")
    Page<Book> searchBooks(Book book);

//    Integer borrowBook(Book book);

    Integer addBook(Book book);
//    Integer editBook(Book book);


    /**
     * 查询已借阅但未归还的图书和所有待确认归还的图书 管理员的方法
     * @param book
     * @return
     */
    @Select({
            "<script>"+
            "select * from book "+
            "where book_borrower = #{borrower} "+
            "and book_status ='1' "+
            "<if test = \"name != null\">\n "+
            "   and book_name like concat('%',#{name},'%') \n"+
            "   </if>"+
            "<if test = \"press != null\">\n"+
            "   and book_press like concat('%',#{press},'%') \n"+
            "   </if>"+
            "<if test = \"author != null\">\n"+
            "   and book_author like concat('%',#{author},'%') \n"+
            "   </if>"+
            "or book_status ='2' "+
            "<if test = \"name != null\">\n"+
            "   and book_name like concat('%',#{name},'%') \n"+
            "   </if>"+
            "<if test = \"press != null\">\n"+
            "   and book_press like concat('%',#{press},'%') \n"+
            "   </if>"+
            "<if test = \"author != null\">\n"+
            "   and book_author like concat('%',#{author},'%') \n"+
            "   </if>"+
            "order by book_borrowtime desc"+
            "</script>"
    })
    @ResultMap("bookMap")
    Page<Book> selectBorrowed(Book book);



    /**
     * 查阅当前用户借阅但未归还的图书 普通用户的方法
     * @param book
     * @return
     */
    @Select({"<script>" +
            "select * from book " +
            "where book_borrower = #{borrower} " +
            "and book_status in ('1','2') " +
            "<if test = \"name != null\">\n"+
            "   and book_name like concat('%',#{name},'%') \n"+
            "   </if>"+
            "<if test = \"press != null\">\n"+
            "   and book_press like concat('%',#{press},'%') \n"+
            "   </if>"+
            "<if test = \"author != null\">\n"+
            "   and book_author like concat('%',#{author},'%') \n"+
            "   </if>"+
            "order by book_borrowtime" +
            "</script>"
    })
    @ResultMap("bookMap")
    Page<Book> selectMyBorrowed(Book book);

}
